package org.example.controller;

import cn.dev33.satoken.util.SaResult;
import org.example.common.entity.Carousel;
import org.example.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/carousel")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;


    // 插入一条轮播图记录
    @PostMapping("/insert")
    public SaResult insert(@RequestBody Carousel carousel) {
        int result = carouselService.insert(carousel);
        return result > 0 ? SaResult.ok("插入成功") : SaResult.error("插入失败");
    }

    // 根据 ID 更新轮播图记录
    @PostMapping("/update")
    public SaResult update(@RequestBody Carousel carousel) {
        int result = carouselService.updateById(carousel);
        return result > 0 ? SaResult.ok("更新成功") : SaResult.error("更新失败");
    }

    // 根据 ID 删除轮播图记录（逻辑删除）
    @DeleteMapping("/delete/{id}")
    public SaResult delete(@PathVariable Long id) {
        int result = carouselService.deleteById(id);
        return result > 0 ? SaResult.ok("删除成功") : SaResult.error("删除失败");
    }

    // 根据 ID 查询轮播图记录
    @GetMapping("/select/{id}")
    public SaResult selectById(@PathVariable Long id) {
        Carousel carousel = carouselService.selectById(id);
        return carousel != null ? SaResult.data(carousel) : SaResult.error("未找到记录");
    }

    // 查询所有未删除的轮播图记录
    @GetMapping("/selectAll")
    public SaResult selectAll() {
        List<Carousel> carousels = carouselService.selectAll();
        return SaResult.data(carousels);
    }

    // 根据平台和状态查询轮播图记录
    @GetMapping("/selectByPlatformAndStatus")
    public SaResult selectByPlatformAndStatus(
            @RequestParam String platform,
            @RequestParam Integer status) {
        List<Carousel> carousels = carouselService.selectByPlatformAndStatus(platform, status);
        return SaResult.data(carousels);
    }
}